<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];
    public function render($request, Throwable $exception)
    {
        if (!$request->is("admin/*")){
            if ($exception instanceof \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException)
                return response()->json(
                    [
                        'code'=>401,
                        'msg'=>'没有权限操作',
                        'data'=>[]
                    ]

                );
            if($exception instanceof  ApiException ){

                return response()->json(
                    [
                        'code'=>201,
                        'msg'=> $exception->getMessage(),
                        'data'=>[]
                    ]
                )->setEncodingOptions(JSON_UNESCAPED_UNICODE);
            }
            if ($exception instanceof  NotFoundHttpException){
                return response()->json(
                    [
                        'code'=>201,
                        'msg'=> '找不到资源',
                        'data'=>[]
                    ]
                )->setEncodingOptions(JSON_UNESCAPED_UNICODE);
            }
        };
        return parent::render($request, $exception);
    }
    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }
}
